package cn.tedu.submarine;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.awt.Graphics;
import java.util.Random;
import  javax.swing.ImageIcon;

public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    public static final int SECOND = 2;
    protected int state = LIVE;

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    public SeaObject(int width, int height) {
        this.width = 63;
        this.height = 19;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT - height - 150 + 1) + 150;
        speed = rand.nextInt(3) + 1;
    }

    public SeaObject(int width, int height, int x, int y, int speed) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void step();


    public abstract ImageIcon getImage();

    public boolean isLive() {
        return state == LIVE;
    }

    public boolean isDead() {
        return state == DEAD;
    }

    public boolean isSecond(){return state == SECOND;}
    public void paintImage(Graphics g) {
        if (getImage()!=null) {
            this.getImage().paintIcon(null, g, this.x, this.y);
        }

    }

    public SeaObject shootThunder() {
        int x = this.x + this.width;
        int y = this.y - 5;
        if (this instanceof TorpedoSubmarine) {
            return new Torpedo(x, y);
        } else if (this instanceof MineSubmarine) {
            return new Mine(x, y);
        } else {
            return null;
        }
    }

    public Boolean isOutOfBounds() {
        return this.x>=World.WIDTH;
    }


    /** 检测碰撞*/
    public Boolean isHit(SeaObject other){
        int x1 = this.x-other.width;
        int x2 = this.x+this.width;
        int y1 = this.y-other.height;
        int y2 = this.y+this.height;
        int x = other.x;
        int y = other.y;
        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

    public void goDead(){
        state = DEAD;
    }
    public void goSecond(){
        state = SECOND;
    }

}
